<?php
class Controller_MM extends Controller_Template 
{
    public function __construct($req, $res)
    {
        $this->_cpid    = NULL;
        $this->template = 'smarty:payment/orders';

        parent::__construct($req, $res);

        $user = user::current_user();
        $role = $user->get_role_id();
        if ($role == 'mmcp')
            $this->_cpid = user::current_user_id();
    }

    public function action_stat ($start_day = NULL, $stop_day = NULL)
    {
        if (empty($start_day))
            $start_day = "2013-08-01";
            //$start_day = date("Y-m-d", time() - 86400 * 30);

        if (empty($stop_day))
            $stop_day  = date("Y-m-d");

        $stats = Payment_MM_Order::orders_by_cp_and_chns ($start_day, $stop_day, $this->_cpid);
        $this->template->set('stats', $stats['cps']);
        $this->template->set('total', $stats['total']);
        $this->template->set('day', array('start' => $start_day, 'stop' => $stop_day));
        if ($this->_cpid > 0)
            $this->template->set_filename('smarty:payment/mmcp/stats');
        else
            $this->template->set_filename('smarty:payment/stats');
    }

    public function action_orders ($status = 'all', $page = 1)
    {
        $limit  = 50;
        $offset = (max(1, $page) - 1) * $limit;
        $query  = $this->request->query();

        $appid  = NULL;
        $cpid   = NULL == $this->_cpid ? Arr::get($query, 'cp') : $this->_cpid;
        $extchn = Arr::get($query, 'extchn');
        $sday   = Arr::get($query, 'start');
        $eday   = Arr::get($query, 'stop');


        $orders = Payment_MM_Order::statistics($cpid, $appid, $status, $extchn, $sday, $eday);
        $result = Payment_MM_Order::orders($cpid, $appid, $status, $extchn, $sday, $eday, $offset, $limit);
        $this->template->set('result', $result);
        $this->template->set('orders', $orders);
        $this->template->set('query', URL::query());
        $this->template->set('pagination', $this->pagination($orders['orders'], 50, 'route', 'param2', 'pagination/simple'));
    }

    public function action_developers ()
    {
        $paycodes = Payment_MM_Channel_Paycode::lists();
        $this->template->set_filename('smarty:payment/paycodes');
        $this->template->set('paycodes', $paycodes);
    }

    public function action_sdk ()
    {
        $cpid = $this->_cpid;
        if (empty($cpid))
            return;

        $paycodes = Payment_MM_Channel_Paycode::lists($cpid);
        $this->template->set_filename('smarty:payment/sdk');
        $this->template->set('cp', Payment_MM_Channel::info($cpid));
        $this->template->set('paycodes', $paycodes);
    }

    public function action_add ($type, $value = NULL)
    {
        $post = $this->request->post();

        if ($type == 'cp')
        {
            if (! empty($post))
            {
                $cpname   = Arr::get($post, 'name', '');
                $passport = Arr::get($post, 'passport', '');
                $mobile   = Arr::get($post, 'mobile', '');
                $nick = Arr::get($post, 'nickname', '');
                $real = Arr::get($post, 'realname', '');

                try {
                    $cpid = Payment_MM_Channel::add($cpname, $passport, $mobile, $nick, $real);
                    $this->template->set('message', "添加CP成功");
                }catch (Exception $e)
                {
                    $this->template->set('message', $e->getMessage());
                }
            }

            $this->template->set_filename('smarty:payment/manage/add/cp');
        }
        else if ($type == 'app')
        {
            $cpid = (int) $value;
            if (! empty($post))
            {
                $appname = Arr::get($post, 'appname', '');
                try {
                    Payment_MM_Channel_App::add($cpid, $appname);
                    $this->template->set('message', '添加产品成功');
                } catch (Exception $e)
                {
                    $this->template->set('message', $e->getMessage());
                }
            }

            $this->template->set_filename('smarty:payment/manage/add/app');
            $this->template->set('cp', Payment_MM_Channel::info($cpid));
        }
        else if ($type == 'paycode')
        {
            $appid = (int) $value;
            if (! empty($post))
            {
                try {

                    $name     = Arr::get($post, 'name', '');
                    $paycode  = Arr::get($post, 'paycode', '');
                    $price    = (int) (Arr::get($post, 'price', 0) * 100);
                    $describe = Arr::get($post, 'describe', '');

                    Payment_MM_Channel_Paycode::add($appid, $name, $paycode, $price, $describe);
                    $this->template->set('message', '添加道具成功');

                }catch (Exception $e)
                {
                    $this->template->set('message', $e->getMessage());
                }
            }

            $this->template->set_filename('smarty:payment/manage/add/paycode');
            $app = Payment_MM_Channel_APP::info($appid);
            $cp  = Payment_MM_Channel::info($app['channel']);
            $this->template->set('app', $app);
            $this->template->set('cp', $cp);

        }
    }
}
